package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-01-15
 */
@RestController
@RequestMapping("/eduservice/teacher")
@Api(description="讲师管理")
@Slf4j
@CrossOrigin
public class EduTeacherController {


    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     *查询所有
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);


        return R.ok().data("items",list);
    }

    /**
     * 通过id查询
     *
     *
     *
     */
    @ApiOperation(value = "通过id查询")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);

        /*

        自定义异常处理

         */
//        try {
//            int i=1/0;
//        } catch (Exception e) {
//            //输出日志到error日志中
//            log.error(e.getMessage());
//            throw new MyException(20001,"自定义异常处理");
//        }

        return teacher==null?R.error():R.ok().data("teacher",teacher);
    }



    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(
            @PathVariable  long current,
            @PathVariable long limit){

        Page<EduTeacher> teacherPage =new Page<>(current,limit);
        eduTeacherService.page(teacherPage,null);

        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        Map<String,Object> map = new HashMap<String,Object>();

        map.put("total",total);
        map.put("记录",records);

        return R.ok().data(map);

    }

    /**
     * 带条件的分页查询
     *
     *
     *      @RequestBody:把jsion数据封装为对象
     *          required = false TeacherQuery对象可以没有，
     *          请求必须是 @PostMapping
    **/
    @ApiOperation(value = "带条件的分页查询")
    @PostMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacherCondition(@PathVariable long current , @PathVariable long limit,
                                      @RequestBody(required = false) TeacherQuery teacherQuery
                                      ){
        //创建page对象
        Page<EduTeacher> teacherPage = new Page<>(current,limit);

        //构建条件
        QueryWrapper<EduTeacher> teacherQueryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //条件
        if(!StringUtils.isEmpty(name)){
            teacherQueryWrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            teacherQueryWrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            teacherQueryWrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(begin)){
            teacherQueryWrapper.le("gmt_modified",end);
        }
        teacherQueryWrapper.orderByDesc("gmt_create");

        //实现分页查询
        eduTeacherService.page(teacherPage,teacherQueryWrapper);


        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();



        if (total!=0){
            return R.ok().data("total",total).data("rows",records);
        }
        return R.error();


    }

    /**
     * 逻辑删除
     */
    @DeleteMapping("{id}")
    @ApiOperation(value = "根据ID删除讲师")
    public R removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){

        boolean flag = eduTeacherService.removeById(id);
        if (flag){
            return  R.ok();
        }

        return R.error();
    }


    /**
     * 添加讲师
     */
    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public R saveTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);

        return flag==true?R.ok():R.error();
    }

    /**
     * 修改讲师
     */

    @ApiOperation(value = "修改讲师")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher){
        boolean flag = eduTeacherService.updateById(teacher);
        return flag==true?R.ok():R.error();
    }

}

